package com.uplift.mgt.customer.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uplift.common.Const;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.mgt.customer.model.Receipts;
import com.uplift.mgt.customer.service.IReceiptsService;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 收款记录
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/receipts")
public class ReceiptsController {

	@Autowired
	private IReceiptsService receiptsService;

	/**
	 * 查询收款记录
	 * 
	 * @param request
	 * @param receipts
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, Receipts receipts) {
		try {
			Map<?, ?> userMap = (Map<?, ?>) request.getSession().getAttribute(Const.SESSION_USER);
			
			if (userMap != null) {
				Object roleObj = userMap.get("role");
				receipts.setRole(Objects.toString(roleObj));
			}
			
			List<Map<String, Object>> list = this.receiptsService.queryReceipts(receipts);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 保存收款记录
	 * 
	 * @param request
	 * @param receipts
	 * @return IResultable
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public IResultable save(HttpServletRequest request, Receipts receipts) {
		try {
			if (this.isExUser(request)) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败，无权操作");
			}
			
			int ret = this.receiptsService.saveReceipts(receipts);
			if (ret == 1) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "保存成功");
			} else if(ret == 2){
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败，不存在客户");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
		}
	}

	/**
	 * 删除收款记录
	 * 
	 * @param request
	 * @param receipts
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, Receipts receipts) {
		try {
			if (this.isExUser(request)) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败，无权操作");
			}
			
			int row = this.receiptsService.removeReceipts(receipts);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}
	
	private boolean isExUser(HttpServletRequest request) {
		boolean ret = false;
		Map<?, ?> userMap = (Map<?, ?>) request.getSession().getAttribute(Const.SESSION_USER);
		if (userMap != null) {
			Object roleObj = userMap.get("role");
			String role = Objects.toString(roleObj);
			if (StringUtils.equals("4", role)) {
				ret = true;
			}
		}
		return ret;
	}

	/**
	 * 导入汇款记录
	 * @return
	 */
	@RequestMapping(value = "/import-receipts")
	@ResponseBody
	public IResultable importReceipts(MultipartHttpServletRequest request) {
		try {
			this.importReceipts(request);
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "导入成功");
		}catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, e.getMessage());
		}
	}
}
